#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_multimap<int, int>hash;
        for (int i = 0; i < nums.size(); i++)
        {
            hash.insert({ nums[i],i });
        }
        for (int i = 0; i < nums.size(); i++)
        {
            auto it = hash.find(nums[i]);

            while (it != hash.end() && it->first == nums[i])
            {
                if (abs(it->second - i) <= k && i != it->second) return true;
                it++;
            }
        }
        return false;
    }
};